package com.cennavi.minenavidemo.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;

import com.minedata.minenavi.map.MapView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.map.TrafficEventInfo;
import com.minedata.minenavi.mapdal.InitializationException;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyMapView extends MapView {
    public interface Listener {
        void onCameraChanged(int changeType);

        void onTrafficEventIconClicked(int type, TrafficEventInfo trafficEventInfo);

        void onScrollStarted();

        void onScrollFinished();
    }

    private ArrayList<Listener> mListeners = new ArrayList<>();

    public void addListener(Listener listener) {
        mListeners.add(listener);
    }

    public MyMapView(Context context) {
        super(context);
    }

    public MyMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        super.onSurfaceCreated(gl10, eglConfig);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        super.onSurfaceChanged(gl, width, height);
    }

    @Override
    public void onCameraChanged(int changeType) {
        super.onCameraChanged(changeType);

        for (Listener listener : mListeners) {
            listener.onCameraChanged(changeType);
        }
    }

    @Override
    public void onTrafficEventIconClicked(int type, TrafficEventInfo trafficEventInfo) {
        super.onTrafficEventIconClicked(type, trafficEventInfo);

        for (Listener listener : mListeners) {
            listener.onTrafficEventIconClicked(type, trafficEventInfo);
        }
    }

    @Override
    public void onScrollStarted() {
        super.onScrollStarted();

        for (Listener listener : mListeners) {
            listener.onScrollStarted();
        }
    }

    @Override
    public void onScrollFinished(boolean isScroll) {
        super.onScrollFinished(isScroll);

        for (Listener listener : mListeners) {
            listener.onScrollFinished();
        }
    }

    @Override
    protected MineMap createMineMap(Rect rct, MineMap.Listener listener) {
        try {
            return new MineMap(getContext(), listener, true);
        } catch (InitializationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
